package Data;


import java.util.ArrayList;

public class UserCategory {
	private ArrayList<UserGroup> allgroups = null;


	public ArrayList<UserGroup> getAllgroups() {
		return allgroups;
	}
	
	public User getUser(String ip){
		User user = null;
		
		for(int loopi=0;loopi<allgroups.size();loopi++){
			user = allgroups.get(loopi).getUser(ip);
			if(user != null)
				break;
		}
		
		return user;
	}

	public void setAllgroups(ArrayList<UserGroup> allgroups) {
		this.allgroups = allgroups;
	}
	public boolean addNewGroup(UserGroup newGroup) {
		for (UserGroup userGroup : allgroups) {
			if(userGroup.getGroupName().equals(newGroup.getGroupName()))
				return false;
		}
		allgroups.add(newGroup);
		return true;
	}
	public void moveUser(int index,String originalGroup,String targetGroup) {
		UserGroup original = null;
		UserGroup target = null;
		int count = 0;
		for (UserGroup currentGroup : allgroups) {
			if(count == 2)
				break;
			else if(currentGroup.getGroupName().equals(originalGroup)) {
				original = currentGroup;
				count++;
			}
			else if (currentGroup.getGroupName().equals(targetGroup)) {
				target = currentGroup;
				count++;
			}
		}
		User selectedUser = original.getTheUser(index);
		original.deleteUser(selectedUser);
		target.addUser(selectedUser);
	}
	public UserCategory() {
		allgroups = new ArrayList<UserGroup>();
	}

}
